package com.sw.code4j;

import com.sw.code4j.vo.ConfigVO;
import com.gwssi.optimus.core.config.OptimusConfig;
import com.gwssi.optimus.core.config.OptimusProperties;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * @author: 张上亚 2021/11/30 17:42
 * @desc: 代码生成工具入口类
 */
public class AppMain {


    public static void main(String[] args) {
        JFrame frame = new JFrame("Java代码生成器Code4J");
        frame.setSize(600, 500);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);
        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {
        panel.setLayout(null);

        int y = 10;
        JTextField dbTypeText = addTextField(panel, y, "数据库类型:");
        y += 30;

        JTextField hostText = addTextField(panel, y, "数据库地址:");
        y += 30;

        JTextField portText = addTextField(panel, y, "端口:");
        y += 30;

        JTextField usernameText = addTextField(panel, y, "用户名:");
        y += 30;

        JTextField passwordText = addTextField(panel, y, "密码:");
        y += 30;

        JTextField databaseText = addTextField(panel, y, "数据库名:");
        y += 30;

        JTextField tableText = addTextField(panel, y, "数据表名:");
        y += 30;

        JTextField rootPackageText = addTextField(panel, y, "根包名:");
        y += 30;

        JTextField rootFolderText = addTextField(panel, y, "存储目录:");
        y += 30;

        JTextField moduleNameText = addTextField(panel, y, "代码模块名:");
        y += 30;

        JButton generateButton = new JButton("一键生成");
        generateButton.setBounds(50, y, 100, 25);
        panel.add(generateButton);
        generateButton.addActionListener(new ActionListener() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent e) {
                String dbType = dbTypeText.getText().trim();
                String host = hostText.getText().trim();
                String port = portText.getText().trim();
                String username = usernameText.getText().trim();
                String password = passwordText.getText().trim();
                String database = databaseText.getText().trim();
                String rootPackage = rootPackageText.getText().trim();
                String rootFolder = rootFolderText.getText().trim();
                String moduleName = moduleNameText.getText().trim();
                String table = tableText.getText().trim();

                if (StringUtils.isBlank(dbType) || StringUtils.isBlank(host) || StringUtils.isBlank(port) || StringUtils.isBlank(username)
                        || StringUtils.isBlank(password) || StringUtils.isBlank(database) || StringUtils.isBlank(rootPackage)
                        || StringUtils.isBlank(rootFolder) || StringUtils.isBlank(moduleName) || StringUtils.isBlank(table)) {
                    JOptionPane.showMessageDialog(panel, "所有文本框必填！");
                    return;
                }

                OptimusProperties properties = new OptimusProperties();
                OptimusConfig optimusConfig = new OptimusConfig();
                optimusConfig.setProperties(properties);

                ConfigVO config = new ConfigVO();
                config.setDbType(dbType);
                config.setModuleName(moduleName);
                config.setRootFolder(rootFolder);
                config.setRootPackage(rootPackage);
                config.setDbName(database);
                config.setHost(host);
                config.setPort(port);
                config.setUsername(username);
                config.setPassword(password);
                config.setTable(table);

                Generator.generate(config);
            }
        });

        JButton openButton = new JButton("打开生成文件夹");
        openButton.setBounds(200, 220, 150, 25);
        panel.add(openButton);
        openButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Desktop.getDesktop().open(new File(rootFolderText.getText().trim()));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    private static JTextField addTextField(JPanel panel, int y, String s) {
        JLabel dbTypeLabel = new JLabel(s);
        dbTypeLabel.setBounds(10, y, 70, 25);
        panel.add(dbTypeLabel);

        JTextField dbTypeText = new JTextField(20);
        dbTypeText.setBounds(90, y, 300, 25);
        panel.add(dbTypeText);
        return dbTypeText;
    }
}
